﻿# -*- coding: utf-8 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from urllib import FancyURLopener

import re

class MyOpener(FancyURLopener):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'

def get_dynamic_download_link(static_url):    
    myopener = MyOpener()
    curl = "http://" + static_url
    doc = myopener.open(curl).read()
    m = re.split('spPreviewLink', doc)
    u = re.split('&#63;', m[1])
    dynamic_link = u[0][8:]
    return dynamic_link

def replace_html_code(old_link):
    p = re.compile("&#\d*;")
    q = p.search(old_link).span()
    all_str = p.findall(old_link)
    total_list = len(all_str)

    for i in range(total_list):
        q = p.search(old_link).span()
        chr_index = int(old_link[q[0]+2:q[1]-1])
        replacedhtmlcode = chr(chr_index)
        old_link = old_link[:q[0]] + replacedhtmlcode + old_link[q[1]:]
    new_link = old_link
    return new_link

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.redirect(replace_html_code(get_dynamic_download_link(self.request.path[1:])))
        
class IndexPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('''<html>
<head>
<title>
skydrie-gae
</title>
<meta content="A Google App Engine application for redirect the skydrive link" name="description">
<meta content="skydrive, skydirve-gae, gae, google app engin," name="keywords">
<meta name="google-site-verification" content="MRuE10bVaHltuPvjYFH0PGfvJAuCZ2FScfAviXiKDFE" />
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4981483-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>
<body>
This another Google App Engine application!<br>
You can use it redirect the skydrive link.<br>
It's very simple:copy your skydrive link after the HOST,Then it's work.<br>
<br>
For Example:<br>
your skydrive link is <br>
http://cid-7978ee0a34b7f662.office.live.com/self.aspx/Public/EMS/2010/0627.mp3<br>
Then the static access link should be <br>
http://skydrive-gae.appspot.com/cid-7978ee0a34b7f662.office.live.com/self.aspx/Public/EMS/2010/0627.mp3<br>

remember remove the skydrive "http://"<br>

Thanks <br>
<img src="http://msl.appspot.com/static/images/gmail.png">
</body>
</html>''')
     
application = webapp.WSGIApplication(
                                     [('/', IndexPage),
                                     ('/index.html', IndexPage),
                                     ('/index.htm', IndexPage),
                                     ('/cid.*', MainPage),
                                     #(r'/http\*',MainPage),
                                     ],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()